package com.example.speech_recognition.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.speech_recognition.R;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.speech.SynthesizeToUrlListener;

public class RecognitionActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_recognition;
    private Button btn_recognition;

    private SpeechSynthesizer mSpeechRecognizer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recognition);
        initView();
        mSpeechRecognizer=SpeechSynthesizer.createSynthesizer(RecognitionActivity.this,mInitListener);

    }

    private void setParameter() {
        // 清空参数
        mSpeechRecognizer.setParameter(SpeechConstant.PARAMS, null);
        mSpeechRecognizer.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); //设置云端
        mSpeechRecognizer.setParameter(SpeechConstant.VOICE_NAME, "xiaoyan");//设置发音人
        mSpeechRecognizer.setParameter(SpeechConstant.SPEED, "50");//设置语速
        //设置合成音调
        mSpeechRecognizer.setParameter(SpeechConstant.PITCH, "50");
        mSpeechRecognizer.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围0~100
        mSpeechRecognizer.setParameter(SpeechConstant.STREAM_TYPE, "3");
        // 设置播放合成音频打断音乐播放，默认为true
//        mSpeechRecognizer.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        mSpeechRecognizer.startSpeaking(et_recognition.getText().toString().trim(), mSynListener);
        int code = mSpeechRecognizer.startSpeaking(et_recognition.getText().toString().trim(), mSynListener);

        if (code != ErrorCode.SUCCESS) {
            if (code == ErrorCode.ERROR_COMPONENT_NOT_INSTALLED) {
                //上面的语音配置对象为初始化时：
                Toast.makeText(RecognitionActivity.this, "语音组件未安装", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(RecognitionActivity.this, "语音合成失败,错误码: " + code, Toast.LENGTH_LONG).show();
            }
        }
    }

    private void initView() {
        et_recognition = (EditText) findViewById(R.id.et_recognition);
        btn_recognition = (Button) findViewById(R.id.btn_recognition);

        btn_recognition.setOnClickListener(this);
    }

    /**
     * 初始化监听器
     */
    private InitListener mInitListener = new InitListener() {
        @Override
        public void onInit(int i) {
            if (i != ErrorCode.SUCCESS) {
                Toast.makeText(RecognitionActivity.this, "初始化失败，错误码：" + i, Toast.LENGTH_SHORT).show();
            }
        }
    };

    /**
     * 合成监听器
     */
    private SynthesizerListener mSynListener = new SynthesizerListener() {

        //开始播放
        @Override
        public void onSpeakBegin() {
            Log.e("onSpeakBegin", "onSpeakBegin: ");
        }

        //缓冲进度回调
        //percent为缓冲进度0~100，beginPos为缓冲音频在文本中开始位置，endPos表示缓冲音频在文本中结束位置，info为附加信息。
        @Override
        public void onBufferProgress(int i, int i1, int i2, String s) {

        }

        //暂停播放
        @Override
        public void onSpeakPaused() {

        }

        //恢复播放回调接口
        @Override
        public void onSpeakResumed() {

        }

        //播放进度回调
        //percent为播放进度0~100,beginPos为播放音频在文本中开始位置，endPos表示播放音频在文本中结束位置.
        @Override
        public void onSpeakProgress(int i, int i1, int i2) {

        }

        //会话结束回调接口，没有错误时，error为null
        @Override
        public void onCompleted(SpeechError speechError) {
            Log.e("onCompleted", "speechError: "+speechError);
        }

        //会话事件回调接口
        @Override
        public void onEvent(int i, int i1, int i2, Bundle bundle) {

        }
    };


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_recognition:
                submit();
                break;
        }
    }


    private void submit() {
        // validate
        String recognition = et_recognition.getText().toString().trim();
        if (TextUtils.isEmpty(recognition)) {
            Toast.makeText(this, "请输入你要合成的文字", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something
        setParameter();
    }
}
